<template>
	<view class="">
		<view class="my-list">
			<u-list @scrolltolower="scrolltolower" v-if="dataList.length > 0">
				<u-list-item v-for="(item, index) in dataList" :key="index" >
					<view 
						class="gift-bag-item" 
						@click="$u.route('/pages/shop/GiftPack/detail', {id:item.id, type:'1'})"
						:style="{background: item.bgColor}"
					>
						<view class="flex flex--row">
							<text>{{item.name}}</text>
							<text class="fsize-24 mt-10">含{{item.projectCount}}种明细</text>
						</view>
						<view class="flex justify-between">
							<text class="fsize-24">{{item.salePrice > 0 ? `￥${item.salePrice}` : '免费'}}</text>
							<view class="flex justify-end">
								<view class="obtn" @click.stop="onDistr(item)">派发</view>
								<view class="obtn" @click.stop="onDel(item.id)" v-if="distributionMethod==='1'">删除</view>
							</view>
						</view>
					</view>
				</u-list-item>
				<u-loadmore :status="loadmore.status" :icon-type="loadmore.iconType" :load-text="loadmore.loadText.loadText" margin-top="20"
					margin-bottom="20" class="coreshop-padding-top-10" />
			</u-list>
		</view>
		<!-- 无数据时默认显示 -->
		<view class="emptybox" v-if="dataList.length === 0">
			<u-empty icon-size="150" text="暂无数据" mode="data"></u-empty>
		</view>
		<u-popup 
			:show="showNumber" 
			mode="center"
			:safeAreaInsetBottom="false"
			round="10"
			@close="onCloseNumber">
			<view class="padding-30 flex column justify-center align-center">
				<u-input placeholder="请输入派发数量" border="surround" v-model="number" type="number" inputAlign="center"/>
				<view class="mt-20 width-200">
					<u-button text="确定" color="#e54d42" @click="onSending"></u-button>
				</view>
			</view>
		</u-popup>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	// import listHeightMixin from '@/common/computedAltitude.js'
	export default {
		// mixins:[listHeightMixin],
		data() {
			return {
				distributionMethod: '1', // 1 多选配发可删除 2 单个配发 不能删除
				shareholderId: null,
				showNumber: false, // 派发次数弹窗
				number: null,
				loadmore:{
					status: 'loadmore',
					iconType: 'flower',
					loadText: {
						loadmore: '轻轻上拉',
						loading: '努力加载中',
						nomore: '实在没有了'
					},
				},
				curNow: 0,
				cardItemId: null,
				dataList: [],
				staffId: null,
				searchData: {
					purpose: 2,
					page: 1,
					limit: 20
				}
			}
		},
		onLoad(opt) {
			if(opt.staffId){
				this.staffId = opt.staffId
				this.distributionMethod = opt.distributionMethod
			}
			this.getPageList()
		},
		methods: {
			onSending(){
				if(!uni.$u.test.isEmpty(this.number)){
					this.$reqJsonPost('StockHolderGiftBagDoCreate', {cardItemId: this.cardItemId, ids:[this.staffId], nums: this.number}).then(res=>{
						this.showNumber = false
						this.number = null
						uni.$emit('upFtBagList', true)
						uni.navigateBack()
					})
				}
			},
			onCloseNumber(){
				this.showNumber = false
				this.number = null
			},
			onDistr(row){
				if(this.distributionMethod === '1'){
					uni.$u.route('/pages/shop/GiftPack/distribute',{cardItemId: row.id})
				}else if(this.distributionMethod === '2'){
					this.cardItemId = row.id
					this.showNumber = true
				}
			},
			onDel(id){
				const that = this
				const ix = that.dataList.findIndex(im=> im.id === id )
				uni.showModal({
					title:'提示',
					content:'确定删除该礼包,此操作将同时删除二次分享的礼包?',
					success:(res=>{
						if(res.confirm){
							that.$reqJsonPost('ProjectCategoryDoDelete', {id: id}).then(res=>{
								that.dataList.splice( ix, 1)
							})
						}
					})
				})
			},
			getPageList() {
				this.$reqPost('GetGiftPackList', this.searchData).then(res => {
					this.dataList = [...this.dataList, ...res.data]
					if (res.count > this.dataList.length) {
						this.loadmore.status = 'loadmore'
						this.searchData.page++
					} else {
						// 数据已加载完毕
						this.loadmore.status = 'nomore'
					}
				})
			},
			scrolltolower() {
				if (this.loadmore.status === 'loadmore') {
					this.getPageList()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	
</style>
